package com.zto.tbschedule.console.controller;

import com.zto.tbschedule.console.help.LoginHelp;
import com.zto.tbschedule.console.service.IApplicationService;
import com.zto.tbschedule.console.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IApplicationService appService;
    @RequestMapping("/list.do")
    @ResponseBody
    public Message<BaseInfo> getMenuList(HttpServletRequest request) {
        List<Menu> menuList = new ArrayList<Menu>();
        User user = LoginHelp.getUser(request);
        menuList.add(getSystemMenu());
        menuList.add(getAppMenu(user));
        if (CollectionUtils.isEmpty(menuList)) {
            menuList.get(0).setActive("active");
        }
        BaseInfo info = new BaseInfo();
        info.setMenuList(menuList);
        info.setUser(LoginHelp.getUser(request));
        info.setDefaultMenuName("系统配置");
        info.setDefaultSubMenuName("用户管理");
        return new Message<BaseInfo>(true, "", info);
    }

    private Menu getAppMenu(User user) {
        Menu menu = new Menu();
        menu.setMenuName("服务列表");
        menu.setIsOpen(true);
        menu.setIcon("fa fa-circle-o");

        List<AppUser> appUserList = appService.getAppListByUid(user.getId());

        if (!CollectionUtils.isEmpty(appUserList)) {
            List<Menu> subMenuList = new ArrayList<Menu>();
            for (AppUser appUser : appUserList) {
                if (appUser.isFlag() || LoginHelp.isAdmin(user)) {
                    Menu subMenu = new Menu();
                    subMenu.setAppId(appUser.getApp().getId());
                    subMenu.setUrl("tasklist.detail({appId:" + appUser.getApp().getId() + "})");
                    subMenu.setMenuName(appUser.getApp().getName());
                    subMenu.setIsOpen(false);
                    subMenuList.add(subMenu);
                }
            }
            menu.setSubMenu(subMenuList);
        }
        return menu;
    }

    private Menu getSystemMenu() {
        Menu menu = new Menu();
        menu.setMenuName("系统配置");
        menu.setIsOpen(true);
        menu.setIcon("fa fa-link");
        menu.setUrl("userlist");
        Menu subMenu1 = new Menu();
        subMenu1.setUrl("userlist");
        subMenu1.setMenuName("用户管理");
        subMenu1.setIsOpen(false);

        Menu subMenu2 = new Menu();
        subMenu2.setUrl("application");
        subMenu2.setMenuName("应用管理");
        subMenu2.setIsOpen(false);
        List<Menu> subMenuList = new ArrayList<Menu>();
        subMenuList.add(subMenu1);
        subMenuList.add(subMenu2);
        menu.setSubMenu(subMenuList);
        return menu;
    }

}
